package com.orangelife.mobile.property.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.jump.JumpManager;
import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.MoneyUtil;
import com.curry.android.util.TimeUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.confirmOrder.biz.ConfirmOrderHttpBiz;
import com.orangelife.mobile.property.activity.PropertyBillHouseListActivity;
import com.orangelife.mobile.property.bean.PropertyBillHistoryListEntity;
import com.orangelife.mobile.property.biz.PropertyHttpBiz;
import com.orangelife.mobile.property.listener.OnOrderHasPaidByOthersListener;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 历史缴费详情的金额列表的适配器
 * <p/>
 * Created by wgf on 2016/5/11.
 *
 * @author wgf
 * @since v3.6.4
 */
public class PropertyBillHistoryListAdapter<T> extends FatherAdapter<T> {
    private Context mContext;
    private List<PropertyBillHistoryListEntity> mList;
    private OnOrderHasPaidByOthersListener mOnOrderHasPaidByOthersListener;

    public PropertyBillHistoryListAdapter(Context context, List<PropertyBillHistoryListEntity> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseAdapter setList(List<T> list) {
        this.mList = (List<PropertyBillHistoryListEntity>) list;
        return this;
    }

    public void setOnOrderHasPaidByOthersListener(OnOrderHasPaidByOthersListener onOrderHasPaidByOthersListener) {
        this.mOnOrderHasPaidByOthersListener = onOrderHasPaidByOthersListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_property_history, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(R.id.tvMoney, viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag(R.id.tvMoney);
        }
        PropertyBillHistoryListEntity entity = mList.get(position);
        String mComm = entity.getHouseInfo();
        String mPmfeeTime = entity.getRange();
        String totalMoney = entity.getTotalCost();
        String orderTime = entity.getOrderTime();
        viewHolder.tvMoney.setText(MoneyUtil.handleMoney(totalMoney) + "元");
        viewHolder.tvCommunity.setText(mComm);
        viewHolder.tvDuration.setText(mPmfeeTime);
        viewHolder.tvDate.setVisibility(View.VISIBLE);
        setStatus(viewHolder.tvStatus, entity.getEnable(), entity.getStatus());
        viewHolder.tvDate.setText(TimeUtil.timeStampToDate(orderTime, "yyyy-MM-dd HH:mm:ss"));
        viewHolder.rlItem.setTag(R.id.rlItem, position);
        viewHolder.rlItem.setOnClickListener(listener);
        return convertView;
    }

    private void setStatus(TextView tv, String enable, String status) {
        tv.setVisibility(View.VISIBLE);
        tv.setText(status);
        if ("0".equals(enable)) {//enable 0 账单已失效,灰色   1 橙色
            tv.setTextColor(mContext.getResources().getColor(R.color.lightGray));
        } else {
            tv.setTextColor(mContext.getResources().getColor(R.color.lightOrange));
        }
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag(R.id.rlItem);
            String toPay = mList.get(pos).getTopay();
            if ("1".equals(toPay)) {/*1,待支付，跳转支付页面；0，其他，跳转详情页面*/
                String commID = mList.get(pos).getCommID();
                String payBillID = mList.get(pos).getOrderID();
                requestOutTradeForPay(commID, payBillID);
            } else {
                jumpToDetail(pos);
            }
        }
    };

    /** 请求支付订单流水号，若成功，则跳转到支付界面 */
    private void requestOutTradeForPay(String commID, String payBillID) {
        ((OrangeLifeBaseActivity) mContext).showDialog((OrangeLifeBaseActivity) mContext);
        PropertyHttpBiz.getOutTradeNoInHistoryList(commID, payBillID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                ((OrangeLifeBaseActivity) mContext).closeDialog();
                ConfirmOrderHttpBiz.handleOutTrade((PropertyBillHouseListActivity) mContext, response);
                if (errCode == 97 && mOnOrderHasPaidByOthersListener != null) {
                    mOnOrderHasPaidByOthersListener.onClick();
                }
            }
        });
    }

    /** 跳转到详情界面 */
    private void jumpToDetail(int pos) {
        JumpManager.jumpToPropertyHistoryDetail(mContext, mList.get(pos).getCommID(), mList.get(pos).getApartmentID(), mList.get(pos).getOrderID());
    }

    static class ViewHolder {
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @Bind(R.id.tvCommunity)
        TextView tvCommunity;
        @Bind(R.id.llCommunity)
        RelativeLayout rlCommunity;
        @Bind(R.id.rlItem)
        LinearLayout rlItem;
        @Bind(R.id.tvMoney)
        TextView tvMoney;
        @Bind(R.id.tvDate)
        TextView tvDate;
        @Bind(R.id.tvStatus)
        TextView tvStatus;
        @Bind(R.id.tvDuration)
        TextView tvDuration;
    }
}
